/*		 
================================================================================
Belonging to Unions Across the World: Macro and Micro Dynamics
Ibrahim Oker, Mehmet Fuat Kina, and Ibrahim Kuran 
================================================================================
*/

/*
================================================================================
NOTE ON DATASET VARIABLES
================================================================================
The dataset provided includes both raw variables from the original survey data
and external sources and constructed variables generated through transformations,
interactions, and IRT models. 

If you wish to replicate the construction of variables from scratch, you should
first DROP all constructed variables before re-running the variable construction
code. This will prevent conflicts and ensure accurate variable generation.


To remove all constructed variables before replication, use:
drop union fullpart immigrant fulltime sex age agesq income educ educ1-educ3 ///
	left leftobj subjsocclass ideology1 ideology2 ideology3 lefttheta2 ///
	classposition instfreedom globalinteg instfreedomsy globalintegsy ///
	democ_stock_liberal90 democ_stock_liberal90p fh_cl_stock fh_pr_stock ///
	fdistock lri_stock trade_stock90 demclass demeducmid demeducmids demeduchigh ///
	demeduchighs demlefts demleftss demlefto demleftoindex demleftirt demleftirts ///
	demclasssy demleftirtssy trclass trideology trclasssy trideologysy ///
	freedominst_std globalinteg_std freedominstsy_std globalintegsy_std

================================================================================
*/

** Variable construction
* Union Membership
gen union=A101
replace union=1 if union>=1 
replace union=. if union<0
replace union=. if A101==.

** Full/Part Time Employment (To select the appropriate sample)
gen fullpart=1 if  X028==1
replace fullpart=1 if  X028==2
* Limit the sample to full- and part-time employers
keep if fullpart==1

** Immigrant
gen immigrant = G027A	
replace immigrant=. if G027A<0
replace immigrant=0 if G027A==1
replace immigrant=1 if G027A==2

** Full/Part Time
gen fulltime=1 if X028==1
replace fulltime=0 if X028==2

** Sex
gen sex= X001
replace sex=. if sex<0

** Age
gen age= X003
replace age=. if age<0
gen agesq=age*age	

** Income
gen income= X047_WVS
replace income=. if X047_WVS<0

** Education
gen educ= X025R
replace educ=. if educ<0
tab educ, gen(educ)

** Ideology subjective
gen left=1 if ideology<5 & ideology!=.
replace left = 0 if ideology>4 & ideology!=.

** Subjective social class
gen subjsocclass= X045
replace subjsocclass=. if subjsocclass<0

** Ideology Subjective ()
gen leftobj = E037
replace leftobj=0 if E037>5
replace leftobj=1 if E037<6
replace leftobj=. if E037<0 

** Alternative ideology measures
* Private vs state ownership
gen ideology1= E036
replace ideology1=. if E036<0

* Competititon
gen ideology2= E039
replace ideology2=. if E039<0

* Self vs. Government care for
gen ideology3 = E037
replace ideology3 = . if E037<0
	
** Create ideology index using IRT (Higher values, more left-leaning)
irt grm ideology1 ideology2 ideology3, vce(cluster ccode)
predict lefttheta2, latent ebmeans

** Create class position index using IRT (Higher values, higher social class)
irt grm subjsocclassr income educ, vce(cluster ccode)
predict classposition, latent ebmeans

** Societal + union freedom
** Since there are continues measures run the IRT model manually
gsem  (PER->  fh_cl_stockr fh_pr_stockr lri_stockr ///
			 democ_stock_liberal90 ,fam(gaussian)link(id) ///
var(PER@1)vce(cluster ccode)) 
predict instfreedom,ebmeans latent se(freedominst_std)
label var instfreedom "Democracy"

** Global Integration Index
gsem (PER -> fdistock trade_stock90, fam(gamma) link(log) ///
    var(PER@1) vce(cluster ccode))
predict globalinteg,ebmeans latent se(globalinteg_std)
	
	
label var democ_stock_liberal90 "Democracy"
label var educ2 "Middle Ed."
label var educ3  "High Ed."
label var immigrant  "Immigrant"
label var left  "Left (Subj.)"
label var lefttheta2  "Left (Obj.)"
label var fulltime  "Full-Time"
label var sex  "Female"
label var age  "Age"
label var agesq  "Age sq."
label var income  "Income"
label var demeducmid  "Democracy X Middle Ed."
label var demeduchigh  "Democracy X High Ed."
label var demlefts  "Democracy X Left (Subj.)"

** Interaction Variables for Better Presentation
gen demeducmid = democ_stock_liberal90 * educ2
gen demeducmids = instfreedom * educ2
label var demeducmids "Democracy X Middle Ed."
gen demeduchigh = democ_stock_liberal90 * educ3
gen demeduchighs = instfreedom * educ3
label var demeduchighs "Democracy X Higher Ed."

gen demlefts = democ_stock_liberal90 * left
gen demleftss = instfreedom * left
label var demleftss "Democracy X Left (Subj.)"

gen demlefto = democ_stock_liberal90 * leftobj
gen demleftoindex = democ_stock_liberal90 * ideologytrans
gen demleftirt = democ_stock_liberal90 * lefttheta2
gen demleftirts = instfreedom * lefttheta2



    

/*
--------------------------------------------------------------------------------
Stock Variables 
--------------------------------------------------------------------------------
*/

* Liberal Democracy Stock Variable --V-Dem
gen democ_stock_liberal90 = . 
bysort ccode (year): replace democ_stock_liberal90 = v2x_libdem  if year >= 1980 & _n == 1
bysort ccode (year): replace democ_stock_liberal90 = v2x_libdem  + 0.99 * democ_stock_liberal90[_n-1] if year >= 1980 & _n > 1`'

* Polity2 Stock Variable
gen democ_stock_liberal90p = . 
bysort ccode (year): replace democ_stock_liberal90p = e_polity2  if year >= 1980 & _n == 1 
bysort ccode (year): replace democ_stock_liberal90p = e_polity2  + 0.99 * democ_stock_liberal90p[_n-1] if year >= 1980 & _n > 1

* FDI Stock Variable
** Civil Liberties
gen fh_cl_stock = .
by ccode: egen first_valid_year = min(cond(year >= 1980 & !missing(fh_cl), year, .))
bysort ccode (year): replace fh_cl_stock = fh_cl if year == first_valid_year
bysort ccode (year): replace fh_cl_stock = fh_cl_stock[_n-1] if missing(fh_cl_stock) & !missing(fh_cl_stock[_n-1])
bysort ccode (year): replace fh_cl_stock = fh_cl + 0.99 * fh_cl_stock[_n-1] if year > first_valid_year

** Political rights
gen fh_pr_stock = .
by ccode: egen first_valid_year = min(cond(year >= 1980 & !missing(fh_pr), year, .))
bysort ccode (year): replace fh_pr_stock = fh_pr if year == first_valid_year
bysort ccode (year): replace fh_pr_stock = fh_pr_stock[_n-1] if missing(fh_pr_stock) & !missing(fh_pr_stock[_n-1])
bysort ccode (year): replace fh_pr_stock = fh_pr + 0.99 * fh_pr_stock[_n-1] if year > first_valid_year


* FDI Stock Variable
gen fdistock = .
by iso: egen first_valid_year = min(cond(year >= 1980 & !missing(fdi), year, .))
bysort iso (year): replace fdistock = fdi if year == first_valid_year
bysort iso (year): replace fdistock = fdistock[_n-1] if missing(fdistock) & !missing(fdistock[_n-1])
bysort iso (year): replace fdistock = fdi + 0.99 * fdistock[_n-1] if year > first_valid_year

* Labor Rights Index Stock
gen LRI_stock = . 
bysort iso (year): replace LRI_stock = lri_overall  if year >= 1980 & _n == 1
bysort iso (year): replace LRI_stock = lri_overall  + 0.99 * LRI_stock[_n-1] if year >= 1980 & _n > 1


*****************************************************************************************************************
*****************************************************************************************************************
***** Figure 1

gen demclass = instfreedom * classposition
label var demclass "Democracy X Class"
label var classposition "Class Position"
label var lefttheta2 "Ideology"
label var demleftirts "Democracy X Ideology"

xtmixed union instfreedom classposition lefttheta2 immigrant  fulltime sex age agesq  || ccode:globalinteg instfreedom,cov(independent) mle 
estimates store model_nointeract
xtmixed union instfreedom classposition demclass lefttheta2 immigrant  fulltime sex age agesq   || ccode: globalinteg instfreedom,cov(independent) mle 
estimates store model_education
xtmixed union instfreedom lefttheta2 demleftirts classposition immigrant fulltime sex age agesq   || ccode: globalinteg  instfreedom,cov(independent) mle 
estimates store model_ideologyo



* 			
coefplot    (model_nointeract, label("Base Model") ///
                msymbol(diamond) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (model_education, label("Class Interac. Model") ///
                msymbol(o) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (model_ideologyo, label("Ideology Interac. Model") ///
                msymbol(square) mlcolor(gs3) msize(vsmall) ///
                ciopts(lcolor(gs3) msize(tiny))), /// 
            drop(_cons) ///
			order(instfreedom /// 
                 classposition lefttheta2 demclass demleftirts immigrant fulltime sex age agesq) /// 
            bylabel("All Variables") ///
            xline(0, lpattern(dash)) /// 
            byopts(labelopts(size(tiny))) /// 
            legend(size(small) position(bottom) cols(4)) ///
            graphregion(fcolor(white) lcolor(white)) /// 
            plotregion(fcolor(white) lcolor(white))



** Predicted Probability Graphs (Figures 3,4,5) 
** Class Position
xtmixed union c.instfreedom##c.classposition lefttheta2 immigrant  fulltime sex age agesq || ccode: globalinteg instfreedom,cov(independent) mle 
margins, dydx(classposition) at(instfreedom=(-2(.5)1.5)) 
marginsplot, scheme(s1mono) recast(line) ///
             recastci(rarea) ciopts(fcolor(gs14)) yline(0, lpattern(dash)) ///
             ytitle("Margial Effect of Class Position") ///
             title("") xtitle("Democracy Stock") byopts(title(""))
		 
margins, at(instfreedom=(-2(.5)1.5) classposition=(-0.84 0.01  0.84)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Democracy Stock") byopts(title(""))
			 

** Ideology 	 
xtmixed union c.instfreedom##c.lefttheta2 classposition immigrant  fulltime sex age agesq || ccode: globalinteg instfreedom,cov(independent) mle 
margins, dydx(lefttheta2) at(instfreedom=(-2(.5)1.5))
marginsplot, scheme(s1mono) recast(line) yline(0, lpattern(dash)) ///
             recastci(rarea) ciopts(fcolor(gs14)) ///
             ytitle("Marginal Effect of Ideology") ///
             title("") xtitle("Democracy Stock") byopts(title(""))
   
margins, at(instfreedom=(-2(.5)1.5) lefttheta2=(-0.71 0.01  0.71)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Democracy Stock") byopts(title(""))

						
			
/*		
------------------------------------------------------------------------------------------------------------------------------------------------------------
Trade Share Models (FIGURE 4)
------------------------------------------------------------------------------------------------------------------------------------------------------------ 
*/

** Interaction Variables for Better Presentation
gen trclass = globalinteg * classposition
gen trideology = globalinteg * lefttheta2

label var trclass "Integration X Class"
label var trideology "Integration X Left"
label var globalinteg "Integration"

 

xtmixed union globalinteg classposition lefttheta2 immigrant fulltime sex age agesq  || ccode: globalinteg instfreedom,cov(independent) mle
estimates store modelt_nointeract
xtmixed union globalinteg classposition trclass lefttheta2 immigrant  fulltime sex age agesq  || ccode: globalinteg instfreedom,cov(independent) mle 
estimates store modelt_education
xtmixed union globalinteg lefttheta2 trideology classposition immigrant  fulltime sex age agesq  || ccode: globalinteg  instfreedom,cov(independent) mle 
estimates store modelt_ideologyo



coefplot    (modelt_nointeract, label("Base Model") ///
                msymbol(diamond) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (modelt_education, label("Class Interac. Model") ///
                msymbol(o) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (modelt_ideologyo, label("Ideology Interac. Model") ///
                msymbol(square) mlcolor(gs3) msize(vsmall) ///
                ciopts(lcolor(gs3) msize(tiny))), ///
			drop(_cons) ///
            order(globalinteg classposition lefttheta2 trclass trideology immigrant fulltime sex age agesq) /// 
            bylabel("All Variables") ///
            xline(0, lpattern(dash)) /// 
            coeflabel(_cons = "Baseline Predicted Prob.") /// 
            byopts(labelopts(size(tiny))) /// 
            legend(size(small) position(bottom) cols(4)) ///
            graphregion(fcolor(white) lcolor(white)) /// 
            plotregion(fcolor(white) lcolor(white))



** Predicted Probability Graphs
** Class Position
xtmixed union c.globalinteg##c.classposition lefttheta2 immigrant fulltime sex age agesq  || ccode: globalinteg  instfreedom,cov(independent) mle 
margins, dydx(classposition) at(globalinteg=(-1.2(0.3)1.5)) 
marginsplot, scheme(s1mono) recast(line)  ///
             recastci(rarea) ciopts(fcolor(gs14)) ///
             ytitle("Marginal Effect of Class Position") ///
             title("") xtitle("Economic Itegration Stock") byopts(title(""))

margins, at(globalinteg=(-1.2(0.3)1.5) classposition=(-0.84 0.01  0.84)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Economic Itegration Stock") byopts(title(""))

			 

			 
** Ideology  
xtmixed union c.globalinteg##c.lefttheta2 classposition immigrant educ fulltime sex age agesq   || ccode: globalinteg  instfreedom,cov(independent) mle 
margins, dydx(lefttheta2) at(globalinteg=(-1.2(0.3)1.5))    
marginsplot, scheme(s1mono) recast(line) yline(0, lpattern(dash)) ///
             recastci(rarea) ciopts(fcolor(gs14)) ///
             ytitle("Marginal Effect of Ideology") ///
             title("") xtitle("Economic Itegration Stock") byopts(title(""))
	 
margins, at(globalinteg=(-1.2(0.3)1.5) lefttheta2=(-0.71 0.01  0.71)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Economic Itegration Stock") byopts(title(""))
			 

/*		
------------------------------------------------------------------------------------------------------------------------------------------------------------
APPENDIX
------------------------------------------------------------------------------------------------------------------------------------------------------------ 
*/


*Appendix Table 1
sum union instfreedom globalinteg instfreedomsy globalintegsy classposition lefttheta2 immigrant  fulltime sex age agesq 

*Figure A1
graph bar union if fullpart==1 & union!=., over(iso)



*===============================================================================
* Macro Variables in the Survey Year
*===============================================================================
** Societal + union freedom
** Since these are continues measures run the IRT model manually
gsem  (PER-> fh_clr fh_prr lri_overallr ///
			 v2x_libdem,fam(gaussian)link(id) ///
var(PER@1)vce(cluster ccode)) 
predict instfreedomsy,ebmeans latent se(freedominstsy_std)
label var instfreedomsy "Democracy"

** Global Integration Index
gsem (PER -> fdilog lntrade, link(log) ///
    var(PER@1) vce(cluster ccode))
predict globalintegsy,ebmeans latent se(globalintegsy_std)
label var globalintegsy "Integration"
*===============================================================================
** Models with new survey year variables
gen demclasssy = instfreedomsy * classposition
label var demclasssy "Democracy X Class"
label var classposition "Class Position"
label var lefttheta2 "Ideology"
gen demleftirtssy = instfreedomsy * lefttheta2
label var demleftirtssy "Democracy X Ideology"

* Figure A2
xtmixed union instfreedomsy classposition lefttheta2 immigrant  fulltime sex age agesq  || ccode:globalintegsy instfreedomsy,cov(independent) mle 
estimates store model_nointeract
xtmixed union instfreedomsy classposition demclasssy lefttheta2 immigrant  fulltime sex age agesq   || ccode: globalintegsy instfreedomsy,cov(independent) mle 
estimates store model_education
xtmixed union instfreedomsy lefttheta2 demleftirtssy classposition immigrant fulltime sex age agesq   || ccode: globalintegsy  instfreedomsy,cov(independent) mle 
estimates store model_ideologyo



* 			
coefplot    (model_nointeract, label("Base Model") ///
                msymbol(diamond) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (model_education, label("Class Interac. Model") ///
                msymbol(o) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (model_ideologyo, label("Ideology Interac. Model") ///
                msymbol(square) mlcolor(gs3) msize(vsmall) ///
                ciopts(lcolor(gs3) msize(tiny))), /// 
            drop(_cons) ///
			order(instfreedomsy /// 
                 classposition lefttheta2 demclasssy demleftirtssy immigrant fulltime sex age agesq) /// 
            bylabel("All Variables") ///
            xline(0, lpattern(dash)) /// 
            byopts(labelopts(size(tiny))) /// 
            legend(size(small) position(bottom) cols(4)) ///
            graphregion(fcolor(white) lcolor(white)) /// 
            plotregion(fcolor(white) lcolor(white))



** Predicted Probability Graphs 
*===============================================================================
*Figures A3 
** Panel a: Class Position
xtmixed union c.instfreedomsy##c.classposition lefttheta2 immigrant  fulltime sex age agesq || ccode: globalintegsy instfreedomsy,cov(independent) mle 
margins, dydx(classposition) at(instfreedomsy=(-1.3(.5)1.3)) 
marginsplot, scheme(s1mono) recast(line) ///
             recastci(rarea) ciopts(fcolor(gs14)) yline(0, lpattern(dash)) ///
             ytitle("Margial Effect of Class Position") ///
             title("") xtitle("Democracy (Survey Year)") byopts(title(""))

* Panel b			 
margins, at(instfreedomsy=(-1.3(.5)1.3) classposition=(-0.84 0.01 0.84)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Democracy (Survey Year)") byopts(title(""))
			 
*===============================================================================
*Figures A4: Ideology 	 
* Panel a
xtmixed union c.instfreedomsy##c.lefttheta2 classposition immigrant  fulltime sex age agesq || ccode: globalintegsy instfreedomsy,cov(independent) mle 
margins, dydx(lefttheta2) at(instfreedomsy=(-1.3(.5)1.3))
marginsplot, scheme(s1mono) recast(line) yline(0, lpattern(dash)) ///
             recastci(rarea) ciopts(fcolor(gs14)) ///
             ytitle("Marginal Effect of Ideology") ///
             title("") xtitle("Democracy (Survey Year)") byopts(title(""))
 
* Panel b
margins, at(instfreedomsy=(-1.3(.5)1.3) lefttheta2=(-0.71 0.01  0.71)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Democracy (Survey Year)") byopts(title(""))

						
			
/*		
------------------------------------------------------------------------------------------------------------------------------------------------------------
Trade Share Models (FIGURE 4)
------------------------------------------------------------------------------------------------------------------------------------------------------------ 
*/

** Interaction Variables for Better Presentation
gen trclasssy = globalintegsy * classposition
gen trideologysy = globalintegsy * lefttheta2

label var trclasssy "Integration X Class"
label var trideologysy "Integration X Left"
label var globalintegsy "Integration"

 
* Figure A5 
* Panel b
xtmixed union globalintegsy classposition lefttheta2 immigrant fulltime sex age agesq  || ccode: globalintegsy instfreedomsy,cov(independent) mle
estimates store modelt_nointeract
xtmixed union globalintegsy classposition trclasssy lefttheta2 immigrant  fulltime sex age agesq  || ccode: globalintegsy instfreedomsy,cov(independent) mle 
estimates store modelt_education
xtmixed union globalintegsy lefttheta2 trideologysy classposition immigrant  fulltime sex age agesq  || ccode: globalintegsy  instfreedomsy,cov(independent) mle 
estimates store modelt_ideologyo



coefplot    (modelt_nointeract, label("Base Model") ///
                msymbol(diamond) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (modelt_education, label("Class Interac. Model") ///
                msymbol(o) mlcolor(gs10) msize(vsmall) ///
                ciopts(lcolor(gs10) msize(tiny))) /// 
            (modelt_ideologyo, label("Ideology Interac. Model") ///
                msymbol(square) mlcolor(gs3) msize(vsmall) ///
                ciopts(lcolor(gs3) msize(tiny))), ///
			drop(_cons) ///
            order(globalintegsy classposition lefttheta2 trclasssy trideologysy immigrant fulltime sex age agesq) /// 
            bylabel("All Variables") ///
            xline(0, lpattern(dash)) /// 
            coeflabel(_cons = "Baseline Predicted Prob.") /// 
            byopts(labelopts(size(tiny))) /// 
            legend(size(small) position(bottom) cols(4)) ///
            graphregion(fcolor(white) lcolor(white)) /// 
            plotregion(fcolor(white) lcolor(white))

*===============================================================================


** Predicted Probability Graphs
* Figure A6
** Class Position
* Panel a
xtmixed union c.globalintegsy##c.classposition lefttheta2 immigrant fulltime sex age agesq  || ccode: globalintegsy  instfreedomsy,cov(independent) mle 
margins, dydx(classposition) at(globalintegsy=(-1.2(0.3)2.18)) 
marginsplot, scheme(s1mono) recast(line)  ///
             recastci(rarea) ciopts(fcolor(gs14)) ///
             ytitle("Marginal Effect of Class Position") ///
             title("") xtitle("Economic Itegration (Survey Year)") byopts(title(""))
* Panel b
margins, at(globalintegsy=(-1.2(0.3)2.18) classposition=(-0.84 0.01  0.84)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Economic Itegration (Survey Year)") byopts(title(""))

*===============================================================================
** Figure A7: Ideology  
* Panel a
xtmixed union c.globalintegsy##c.lefttheta2 classposition immigrant educ fulltime sex age agesq   || ccode: globalintegsy  instfreedomsy,cov(independent) mle 
margins, dydx(lefttheta2) at(globalintegsy=(-1.2(0.3)2.18))    
marginsplot, scheme(s1mono) recast(line) yline(0, lpattern(dash)) ///
             recastci(rarea) ciopts(fcolor(gs14)) ///
             ytitle("Marginal Effect of Ideology") ///
             title("") xtitle("Economic Itegration (Survey Year)") byopts(title(""))

* Panel b
margins, at(globalintegsy=(-1.2(0.3)2.18) lefttheta2=(-0.71 0.01  0.71)) 
marginsplot, scheme(s1mono) noci ///
              ciopts(fcolor(gs14)) ///
             ytitle("Predicted Probability of Union Membership") ///
             title("") xtitle("Economic Itegration (Survey Year)") byopts(title(""))
			 

			 
*===============================================================================
* Appendix C
* Note: The coefficients presented in Appendix C are extracted from the models below. 
* For details, please see the text. 		 
			 
** Create ideology index using IRT (Higher values, more left-leaning)
irt grm ideology1 ideology2 ideology3, vce(cluster ccode)
predict lefttheta2, latent ebmeans

** Create class position index using IRT (Higher values, higher social class)
irt grm subjsocclassr income educ, vce(cluster ccode)
predict classposition, latent ebmeans

** Societal + union freedom
** Since there are continues measures run the IRT model manually
gsem  (PER->  fh_cl_stockr fh_pr_stockr lri_stockr ///
			 democ_stock_liberal90 ,fam(gaussian)link(id) ///
var(PER@1)vce(cluster ccode)) 
predict instfreedom,ebmeans latent se(freedominst_std)
label var instfreedom "Democracy"

** Global Integration Index
gsem (PER -> fdistock trade_stock90, fam(gamma) link(log) ///
    var(PER@1) vce(cluster ccode))
predict globalinteg,ebmeans latent se(globalinteg_std)
	


